#include"UASGame.h"
#include"IUASRenderableObj.h"

UASObjManager* UASObjManager::_instance = NULL;


void UASObjManager::CleanUp()
{
	if(UASObjManager::_instance!=NULL)
	{
		delete UASObjManager::_instance;
		UASObjManager::_instance =NULL;
	}
}


UASObjManager* UASObjManager::GetInstance()
{
	if(_instance == NULL)
	{
		_instance = new UASObjManager();
	}
	return _instance;
}


void UASObjManager::InsertObject(UASObject* pCurObj)
{
	assert(pCurObj);
	_objList.push_back(pCurObj);
}

void UASObjManager::RemoveObject(UASObject* pCurObj)
{
	assert(pCurObj);

	for(vector<UASObject*>::iterator it = _objList.begin();it!=_objList.end();++it)
	{
		if(*it == pCurObj)
		{
			_objList.erase(it);
			break;
		}
	}
}

void UASObjManager::Tick(float timeDelta)
{
	for(vector<UASObject*>::iterator it = _objList.begin();it!= _objList.end();++it)
	{
		(*it)->Tick(timeDelta);
	}
}

void UASObjManager::Render()
{
	IUASRenderableObj* pRenderObj = NULL;
	for(vector<UASObject*>::iterator it = _objList.begin();it!= _objList.end();++it)
	{
		pRenderObj = dynamic_cast<IUASRenderableObj*>(*it);
		if(pRenderObj!=NULL && pRenderObj->IsShouldBeRender())
		{
			pRenderObj->DoRender();
		}
	}
}



UASGame::UASGame():_pSpritePool(new UASSpritePool())
{
	_pObjManager = UASObjManager::GetInstance();
	assert(_pObjManager!=NULL);

	assert(_pSpritePool);
}

UASGame::~UASGame()
{
	if(_pObjManager!=NULL)
	{
		//delete _pObjManager;
		UASObjManager::CleanUp();
		_pObjManager = NULL;
	}

	if(_pSpritePool!=NULL)
	{
		delete _pSpritePool;
		//_pSpritePool = NULL;
	}
}

void UASGame::Init()
{
	//	test code
	/*_pMannySprite = new UASSprite(g_pd3dDevice,"..\\content\\iceage_image\\sprites\\bonus.dds");
	_pMannySprite->LoadAuroraSprite("..\\content\\iceage_image\\sprites\\bonus.sprite");*/

	/*_pMannySprite = new UASSprite(g_pd3dDevice,"..\\content\\iceage_image\\sprites\\bonus.dds");
	_pMannySprite->LoadAuroraSprite("..\\content\\iceage_image\\sprites\\bonus.sprite");*/
	/*_pMannySprite = new UASSprite(g_pd3dDevice,"..\\content\\iceage_image\\sprites\\manny.png");
	_pMannySprite->LoadAuroraSprite("..\\content\\iceage_image\\sprites\\manny.sprite");*/

	_pMannySprite = _pSpritePool->GetSpriteById(ESFI_GANGSTAR_MAN);
	_paintingAFrameIdx = 0;
	_nRenderingAnimFrames = _pMannySprite->_allAnims[_renderingAnimIdx]._allAFrames.size();

	_xPos = 100;
	_yPos = 100;
}

void UASGame::CleanUp()
{

}

void UASGame::Tick(float timeDelta)
{
	if(_pObjManager != NULL)
	{
		_pObjManager->Tick(timeDelta);
	}

	//	test code
	static float timeCount = 0;
	
	if((timeCount += timeDelta)>0.15f)
	{
		timeCount = 0;
		if(++_paintingAFrameIdx >= _nRenderingAnimFrames)
		{
			_paintingAFrameIdx = 0;
		}
		_yPos += 3;
	}
}

void UASGame::Render()
{
	if(_pObjManager!= NULL)
	{
		_pObjManager->Render();
	}

	
	//	test code .Render Manny.
	//void UASSprite::PaintFrame(IDirect3DDevice9* const device,int curFrameIdx,int x,int y)
	/*_pMannySprite->PaintFrame(g_pd3dDevice,0,100,100);*/
	
	/*_pMannySprite->Render_2D(g_pd3dDevice,0,0,_pMannySprite->_width,_pMannySprite->_height,
							0,0,_pMannySprite->_width,_pMannySprite->_height,
							D3DCOLOR_RGBA(255,255,255,255),D3DCOLOR_RGBA(255,255,255,255),D3DCOLOR_RGBA(255,255,255,255),D3DCOLOR_RGBA(255,255,255,255),
							true,0.5);*/
	//_pMannySprite->PaintFrame(g_pd3dDevice,1,100,100);

	_pMannySprite->PaintAFrame(g_pd3dDevice,
								_pMannySprite->_allAnims[_renderingAnimIdx]._allAFrames[_paintingAFrameIdx]._frameId,
								_xPos,_yPos,0,0);

}


